package handlers

import (
	"encoding/json"
	"fmt"
	"io"
	"luntan/pkg/database"
	"luntan/pkg/models"
	"net/http"
)

type CommitMessage struct {
	Status int `json:"status"`
	// say something to notfy
	Msg string `json:"msg,omitempty"`
	// pass data to js
	Data models.Detail `json:"detail,omitempty"`
}

func GetDetail(w http.ResponseWriter, r *http.Request) {
	msg := CommitMessage{}
	id := r.URL.Query().Get("id")
	if id == "" {
		msg.Status = -1
		msg.Msg = "wrong post id"
		ComtResp(w, &msg)
		return
	}
	var detail models.Detail
	var List models.List
	row := database.SelectIdList(id)
	row.Scan(&List.Id, &List.Owner, &List.Title, &List.Description, &List.Content)
	detail.Id = List.Id
	detail.Owner = List.Owner
	detail.Title = List.Title
	detail.Content = List.Content

	rows, err := database.SelectArticle(id)
	if err != nil {
		msg.Status = -1
		msg.Msg = "select commit wrong"
		ComtResp(w, &msg)
		return
	}
	for rows.Next() {
		var cmt models.Commit
		rows.Scan(&cmt.Id, &cmt.Name, &cmt.Agree, &cmt.Disagree, &cmt.Time, &cmt.Content, &cmt.Ischild, &cmt.Father)
		// fmt.Println(cmt)
		if cmt.Ischild {
			detail.Commentlist[cmt.Father-1].Childcommentlist = append(detail.Commentlist[cmt.Father-1].Childcommentlist, cmt)
			continue
		} else {
			detail.Commentlist = append(detail.Commentlist, cmt)
		}
	}
	msg.Data = detail
	// fmt.Println(detail)
	ComtResp(w, &msg)
}

func CreatCommit(w http.ResponseWriter, r *http.Request) {
	msg := CommitMessage{}
	var cmt models.Commit
	id := r.URL.Query().Get("id")
	if id == "" {
		msg.Status = -1
		msg.Msg = "wrong post id"
		ComtResp(w, &msg)
		return
	}
	buf, err := io.ReadAll(r.Body)
	if err != nil || buf == nil {
		msg.Status = -1
		fmt.Println("reqwrong")
		msg.Msg = "invalid/nil request param"
		ComtResp(w, &msg)
		return
	}
	err = json.Unmarshal(buf, &cmt)
	if err != nil {
		msg.Status = -400
		fmt.Println("jsonwrong")
		fmt.Println(err.Error())
		msg.Msg = err.Error()
		ComtResp(w, &msg)
		return
	}

	session, _ := Session_store.Get(r, "session-luntan")
	sid, _ := session.Values["sid"].(string)
	if sid == "" {
		msg.Status = -1
		msg.Msg = "login first"
		ComtResp(w, &msg)
		return
	}
	// Name             string   `json:"commiter"`
	// Content          string   `json:"content"`
	cmt.Name = session.Values["name"].(string)
	database.CreateCommit(id, cmt)
	msg.Msg = "Commit success"
	ComtResp(w, &msg)
}

func ReplyCommit(w http.ResponseWriter, r *http.Request) {
	msg := CommitMessage{}
	var cmt models.Commit
	id := r.URL.Query().Get("id") //这是文章的id
	if id == "" {
		msg.Status = -1
		msg.Msg = "wrong post id"
		ComtResp(w, &msg)
		return
	}
	buf, err := io.ReadAll(r.Body)
	if err != nil || buf == nil {
		msg.Status = -1
		fmt.Println("reqwrong")
		msg.Msg = "invalid/nil request param"
		ComtResp(w, &msg)
		return
	}
	//需要传回复的id是几，name以及content
	// Name             string   `json:"commiter"`
	// Content          string   `json:"content"`
	// Father           int64    `json:"fatherid,omitempty"`
	jsonMap := make(map[string]interface{})
	err = json.Unmarshal(buf, &jsonMap)
	if err != nil {
		msg.Status = -400
		fmt.Println("jsonwrong")
		fmt.Println(err.Error())
		msg.Msg = err.Error()
		ComtResp(w, &msg)
		return
	}
	cmt.Content = jsonMap["content"].(string)
	cmt.Father = int64(jsonMap["father"].(float64))
	session, _ := Session_store.Get(r, "session-luntan")
	sid, _ := session.Values["sid"].(string)
	if sid == "" {
		msg.Status = -1
		msg.Msg = "login first"
		ComtResp(w, &msg)
		return
	}
	cmt.Name = session.Values["name"].(string)
	cmt.Ischild = true
	database.CreateReplyCommit(id, cmt, 1)
	msg.Msg = "Commit success"
	ComtResp(w, &msg)
}
